package com.heima.reggie.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.heima.reggie.bean.Category;
import com.heima.reggie.bean.PageParam;
import com.heima.reggie.exception.CustomException;
import com.heima.reggie.mapper.CategoryMapper;
import com.heima.reggie.service.CategoryService;
import com.heima.reggie.service.DishService;
import com.heima.reggie.service.SetmealService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;

@Service
@Transactional
public class CategoryServiceImpl implements CategoryService {
    @Autowired
    private CategoryMapper mapper;
    @Autowired
    private DishService dishService;
    @Autowired
    private SetmealService setmealService;
    /**
      * 分页查询
      * @param pageParam
      * @param
      * @return IPage<Category>
      */
    @Override
    public IPage<Category> selectByPage(PageParam pageParam) {
        IPage<Category> page=new Page<>(pageParam.getPage(),pageParam.getPageSize());
        LambdaQueryWrapper<Category> lqw = new LambdaQueryWrapper<>();
        lqw.orderByAsc(Category::getSort);
        return mapper.selectPage(page,lqw);
    }
/**
  * 根据id删除
  * @param
  * @param
  * @return
  */
    @Override
    public int deleteById(Category category) {
        Long id = category.getId();
        Integer type = category.getType();
        if (type==1){
            long cpuntByCid = dishService.findCpuntByCid(id);
            if (cpuntByCid > 0) {
                throw new CustomException("当前分类下关联了菜品，不能删除");
            }
        }
        else {
            long cpuntByCid1 = setmealService.findCpuntByCid(id);
            if (cpuntByCid1 > 0) {
                throw new CustomException("当前分类下关联了套餐，不能删除");//已经关联套餐，抛出一个业务异常
            }
        }

        return mapper.deleteById(id);
    }
/**
  * 新增
  * @param
  * @param
  * @return
  */
    @Override
    public int add(Category category) {
        return mapper.insert(category);
    }
/**
  * 更新
  * @param
  * @param
  * @return
  */
    @Override
    public int update(Category category) {
        return mapper.updateById(category);
    }
/**
  * 根据类型id查询列表
  * @param
  * @param
  * @return
  */
    @Override
    public List<Category> selectDishList(int tid) {
        LambdaQueryWrapper<Category> lqw = new LambdaQueryWrapper<>();
        lqw.select(Category::getId,Category::getName);
        lqw.eq(Category::getType, tid);
        return mapper.selectList(lqw);
    }
/**
  * 根据id查询
  * @param
  * @param
  * @return
  */
    @Override
    public Category selectById(Long id) {
        return mapper.selectById(id);
    }
/**
  * 查询所有
  * @param
  * @param
  * @return
  */
    @Override
    public List<Category> select() {
        return mapper.selectList(null);
    }

}
